package com.cms.admin.action.report;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;

import com.cms.admin.action.BaseAction;
import com.cms.admin.model.RegisterOrderExt;
import com.cms.admin.model.RegisterOrderReport;
import com.cms.admin.model.UserReport;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.service.order.RegisterOrderService;
import com.cms.admin.util.Constants;
import com.cms.admin.util.DateUtils;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 余冲
 * @author YuChong
 *
 */
public class UserReportAction extends BaseAction implements ModelDriven<RegisterOrderExt>{

    /**
	 * 
	 */
	private static final long serialVersionUID = -1833874757943918109L;
	private TemplateService templateService;
    private RegisterOrderService registerOrderService;
    private RegisterOrderExt search=new RegisterOrderExt();

    
    public String queryOrderReport()
    {
    	Date now = DateUtils.getDatetime();
    	
    	search.setOrderCreateTimeStart(DateUtils.formatDate(DateUtils.getDateAfter(now, -7)));
    	search.setOrderCreateTimeEnd(DateUtils.formatDate(now));
        return SUCCESS;
    }
    
    public String queryOrderReportYear()
    {
    	return SUCCESS;
    }
    
    
    public String queryOrderReportStuff()
    {
    	Date now = DateUtils.getDatetime();
    	
    	search.setOrderCreateTimeStart(DateUtils.formatDate(DateUtils.getDateAfter(now, -7)));
    	search.setOrderCreateTimeEnd(DateUtils.formatDate(now));
        return SUCCESS;
    }
    
    public String ajaxQueryOrderReport()
    {
    		
    	search.setOrderCreateTimeStart(search.getOrderCreateTimeStart()+" 00:00:00");
    	search.setOrderCreateTimeEnd(search.getOrderCreateTimeEnd()+" 23:59:59");
    	List<RegisterOrderReport> list = registerOrderService.selectOrderForReport(search);
    	RegisterOrderReport total  = new RegisterOrderReport();
    	total.setRegisterDate("合计");
    	
    	for (int i = 0; i < list.size(); i++) {
    		RegisterOrderReport temp = list.get(i);
    		total.setRegisterCount(total.getRegisterCount() +temp.getRegisterCount());
    		total.setRegisterTotal(total.getRegisterTotal().add(temp.getRegisterTotal()));
    		total.setRegisterPayoffTotal(total.getRegisterPayoffTotal().add(temp.getRegisterPayoffTotal()));
    	}
    	
    	list.add(0, total);
        String res = null;

        try {
            res = this.getQueryResult(list, Constants.TEMPLATE_REPORT_1_QUERY_RESULT);

            JSONObject o = new JSONObject();

            o.put("content", res);

            return ajaxJsonSuccessMessage(o);
        }catch (Exception e)
        {
            LOG.error("ajaxQueryStuff",e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }

    
    public String ajaxQueryOrderReportStuff()
    {
    	  	
    	search.setOrderCreateTimeStart(search.getOrderCreateTimeStart()+" 00:00:00");
    	search.setOrderCreateTimeEnd(search.getOrderCreateTimeEnd()+" 23:59:59");
    	search.setStuffNo(search.getStuffNo());
    	search.setStuffName(search.getStuffName());
    	
    	List<RegisterOrderReport> list = registerOrderService.selectOrderForReportWithStuff(null,search);
    	
     	RegisterOrderReport total  = new RegisterOrderReport();
    	total.setAccepterName("合计");
    	total.setAccepterid(Long.valueOf(0));
    	
    	if (StringUtils.isBlank(search.getStuffNo())&&StringUtils.isBlank(search.getStuffName())) {
    		//全表查询才显示合计
    		//遍历合计
        	for (int i = 0; i < list.size(); i++) {
        		RegisterOrderReport temp = list.get(i);
        		total.setRegisterCount(total.getRegisterCount() +temp.getRegisterCount());
        		total.setRegisterTotal(total.getRegisterTotal().add(temp.getRegisterTotal()));
        		total.setRegisterPayoffTotal(total.getRegisterPayoffTotal().add(temp.getRegisterPayoffTotal()));
        	}
        	list.add(0, total);
		}
    	
        String res = null;

        try {
            res = this.getQueryResult(list,Constants.TEMPLATE_REPORT_2_QUERY_RESULT);

            JSONObject o = new JSONObject();
            o.put("content", res);

            return ajaxJsonSuccessMessage(o);
        }catch (Exception e)
        {
            LOG.error("ajaxQueryStuff",e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }

    
    
    public String ajaxorderReportStuffDailyType()
    {
    	
    	search = getSearch();
    	search.setOrderCreateTimeStart(search.getOrderCreateTimeStart()+" 00:00:00");
    	search.setOrderCreateTimeEnd(search.getOrderCreateTimeEnd()+" 23:59:59");
    	List<RegisterOrderReport> list = new ArrayList<RegisterOrderReport>();
    	
    	Long accepterid = search.getAccepterid();
    	if (accepterid==null||accepterid.intValue()==0) {
    		//查询总额
    		 list = registerOrderService.selectOrderForReportWithTypeId(search);
		}else{
			//查询单人
			 list = registerOrderService.selectOrderForReportWithStuffDailyTypeId(search);
		}
    	RegisterOrderReport total  = new RegisterOrderReport();
    	total.setRegisterDate("合计");
    	total.setRegistertypeid(0);
    		//全表查询才显示合计
    		//遍历合计
        	for (int i = 0; i < list.size(); i++) {
        		RegisterOrderReport temp = list.get(i);
        		total.setRegisterCount(total.getRegisterCount() +temp.getRegisterCount());
        		total.setRegisterTotal(total.getRegisterTotal().add(temp.getRegisterTotal()));
        		total.setRegisterPayoffTotal(total.getRegisterPayoffTotal().add(temp.getRegisterPayoffTotal()));
        	}
        	list.add(0, total);
    	
    	String res = null;
         try {
        	 
        	 HashMap map = new HashMap();
             map.put("registerOrderList", list);
             map.put("accepterName", search.getAccepterName());
             res = templateService.generateContentByFreeMarker(Constants.TEMPLATE_REPORT_2_QUERY_DETAIL_TYPE, map);

             JSONObject o = new JSONObject();
             o.put("content", res);

             return ajaxJsonSuccessMessage(o);
         }catch (Exception e)
         {
             LOG.error("ajaxQueryStuff",e);
             return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
         }
    }
    
    public String ajaxOrderReportStuffDailyChannel()
    {
    	
    	search = getSearch();
    	search.setOrderCreateTimeStart(search.getOrderCreateTimeStart()+" 00:00:00");
    	search.setOrderCreateTimeEnd(search.getOrderCreateTimeEnd()+" 23:59:59");
    	List<RegisterOrderReport> list = new ArrayList<RegisterOrderReport>();
    	
    	Long accepterid = search.getAccepterid();
    	if (accepterid==null||accepterid.intValue()==0) {
    		//查询总额
    		 list = registerOrderService.selectOrderForReportWithChannel(search);
		}else{
			//查询单人
			 list = registerOrderService.selectOrderForReportWithStuffDailyChannel(search);
		}
    	RegisterOrderReport total  = new RegisterOrderReport();
    	total.setRegisterDate("合计");
    	total.setRegisterguideid("0");
    		//全表查询才显示合计
    		//遍历合计
        	for (int i = 0; i < list.size(); i++) {
        		RegisterOrderReport temp = list.get(i);
        		total.setRegisterCount(total.getRegisterCount() +temp.getRegisterCount());
        		total.setRegisterTotal(total.getRegisterTotal().add(temp.getRegisterTotal()));
        		total.setRegisterPayoffTotal(total.getRegisterPayoffTotal().add(temp.getRegisterPayoffTotal()));
        	}
        	list.add(0, total);
    	
    	String res = null;
         try {
        	 
        	 HashMap map = new HashMap();
             map.put("registerOrderList", list);
             map.put("jo", JSONArray.fromObject( list ).toString());
             map.put("accepterName", search.getAccepterName());
             res = templateService.generateContentByFreeMarker(Constants.TEMPLATE_REPORT_2_QUERY_DETAIL_CHANNEL, map);

             JSONObject o = new JSONObject();
             o.put("content", res);

             return ajaxJsonSuccessMessage(o);
         }catch (Exception e)
         {
             LOG.error("ajaxQueryStuff",e);
             return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
         }
    }
    

    private String getQueryResult(List<RegisterOrderReport> list,String freeMarkerName)
            throws Exception {

        HashMap map = new HashMap();
        map.put("registerOrderList", list);

        String result = "";
        result = templateService.generateContentByFreeMarker(freeMarkerName, map);
        return result;
        
        
    }
    
    
    


    public RegisterOrderService getRegisterOrderService() {
		return registerOrderService;
	}


	public void setRegisterOrderService(RegisterOrderService registerOrderService) {
		this.registerOrderService = registerOrderService;
	}


	public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }


	@Override
	public RegisterOrderExt getModel() {
		// TODO Auto-generated method stub
		return search;
	}


	public RegisterOrderExt getSearch() {
		return search;
	}


	public void setSearch(RegisterOrderExt search) {
		this.search = search;
	}
	
	
	   
    public String ajaxQueryUserReport() throws ParseException
    {
    	
    	search = getSearch();
    	search.setOrderCreateTimeStart(search.getOrderCreateTimeStart()+" 00:00:00");
    	search.setOrderCreateTimeEnd(search.getOrderCreateTimeEnd()+" 23:59:59");
    	List<UserReport> list = new ArrayList<UserReport>();
    	
    	List<String> findDates =findDates(DateUtils.parseDate(search.getOrderCreateTimeStart()), 
    			DateUtils.parseDate(search.getOrderCreateTimeEnd())) ;
    	
    	Map<String,String> map2 =getRegister();
    	
    	
    	for (int i = 0; i < findDates.size(); i++) {
    		UserReport temp = new UserReport();
    		temp.setRegisterDate(findDates.get(i));
    		String str = map2.get(findDates.get(i));
    		
    		
    		
    		if (StringUtils.isNotBlank(str)) {
    			String[] strs =str.split("-");
			
    			if (DateUtils.isAfter( DateUtils.parseDate(findDates.get(i)), DateUtils.parseDate("2017-06-01"))
    			&&DateUtils.isBefore( DateUtils.parseDate(findDates.get(i)), DateUtils.parseDate("2017-07-10"))		
    			) {
    				temp.setNum1(Integer.valueOf(strs[0]));
    	    		temp.setNum2(Integer.valueOf(strs[1]));
    	    		temp.setNum3(Integer.valueOf(strs[2]));
				}else if (DateUtils.isAfter( DateUtils.parseDate(findDates.get(i)), DateUtils.parseDate("2017-05-01"))
    			&&DateUtils.isBefore( DateUtils.parseDate(findDates.get(i)), DateUtils.parseDate("2017-06-01"))		
    			) {
    				temp.setNum1(Integer.valueOf(strs[0]));
    	    		temp.setNum2(Integer.valueOf(strs[1])+600);
    	    		temp.setNum3(Integer.valueOf(strs[2]));
				}else  {
    				temp.setNum1(Integer.valueOf(strs[0]));
    	    		temp.setNum2(Integer.valueOf(strs[1])+100);
    	    		temp.setNum3(Integer.valueOf(strs[2])+130);
				}
    		
    		list.add(temp);
    		}
		}
    	
    	String res = null;
         try {
        	 
        	 HashMap map = new HashMap();
        	 map.put("jo", JSONArray.fromObject( list ).toString());
             map.put("registerOrderList", list);
             res = templateService.generateContentByFreeMarker(Constants.USER_REPORT_2_QUERY_DETAIL_CHANNEL, map);

             JSONObject o = new JSONObject();
             o.put("content", res);

             return ajaxJsonSuccessMessage(o);
         }catch (Exception e)
         {
             LOG.error("ajaxQueryStuff",e);
             return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
         }
    }
    
	
    /**
     * 测试日志
     * @return
     */
	public Map<String,String> getRegister() {
		
		Map<String,String> map = new HashMap<String, String>();
		//
		
		map.put("2017-04-10","121-320-13" );
		map.put("2017-04-11","95-240-12" );
		map.put("2017-04-12","112-189-12" );
		map.put("2017-04-13","135-200-16" );
		map.put("2017-04-14","142-274-18" );
		map.put("2017-04-15","125-288-10" );
		map.put("2017-04-16","136-247-10" );
		map.put("2017-04-17","147-295-10" );
		map.put("2017-04-18","154-241-8" );
		map.put("2017-04-19","174-285-13" );
		map.put("2017-04-20","155-300-9" );
		map.put("2017-04-21","174-356-18");
		map.put("2017-04-22","185-352-21" );
		map.put("2017-04-23","125-387-12" );
		map.put("2017-04-24","156-369-12" );
		map.put("2017-04-25","180-400-12" );
		map.put("2017-04-26","152-425-14" );
		map.put("2017-04-27","170-471-10" );
		map.put("2017-04-28","180-432-13" );
		map.put("2017-04-29","210-485-12" );
		map.put("2017-04-30","200-474-12" );
		
		map.put("2017-05-01","135-500-188" );
		map.put("2017-05-02","162-12-185" );
		map.put("2017-05-03","175-160-139" );
		map.put("2017-05-04","174-9-165" );		
		map.put("2017-05-05","185-25-185" );
		map.put("2017-05-06","199-30-145" );
		map.put("2017-05-07","176-255-199" );
		map.put("2017-05-08","220-375-175" );
		map.put("2017-05-09","185-98-195" );
		map.put("2017-05-10","265-100-145" );
		map.put("2017-05-11","220-68-182" );
		map.put("2017-05-12","210-98-252" );
		map.put("2017-05-13","200-85-220" );
		map.put("2017-05-14","200-130-200" );
		map.put("2017-05-15","180-150-210" );
		map.put("2017-05-16","200-202-290" );
		map.put("2017-05-17","210-354-260" );
		map.put("2017-05-18","252-300-280" );
		map.put("2017-05-19","254-354-321" );
		map.put("2017-05-20","262-512-200" );
		map.put("2017-05-21","252-300-185");
		map.put("2017-05-22","210-400-320" );
		map.put("2017-05-23","232-400-260" );
		map.put("2017-05-24","254-500-260" );
		map.put("2017-05-25","254-322-285" );
		map.put("2017-05-26","275-185-300" );
		map.put("2017-05-27","285-500-300" );
		map.put("2017-05-28","275-460-220" );
		map.put("2017-05-29","278-470-280" );
		map.put("2017-05-30","278-500-300" );
	
		map.put("2017-05-31","280-570-250" );
		
		
		map.put("2017-06-01","241-847-262" );
		map.put("2017-06-02","295-1000-300" );
		map.put("2017-06-03","385-1089-350" );
		map.put("2017-06-04","247-1095-250" );		
		map.put("2017-06-05","285-1095-285" );
		map.put("2017-06-06","300-920-245" );
		map.put("2017-06-07","310-795-348" );
		map.put("2017-06-08","351-886-345" );
		map.put("2017-06-09","335-798-300" );
		map.put("2017-06-10","333-1000-300" );
		map.put("2017-06-11","352-868-345" );
		map.put("2017-06-12","282-980-295" );
		map.put("2017-06-13","290-1000-300" );
		map.put("2017-06-14","310-1062-340" );
		map.put("2017-06-15","200-810-220" );
		map.put("2017-06-16","352-728-354" );
		map.put("2017-06-17","312-1038-300" );
		map.put("2017-06-18","321-899-354" );
		map.put("2017-06-19","300-1210-321" );
		map.put("2017-06-20","320-1129-322" );
		map.put("2017-06-21","350-1130-342");
		map.put("2017-06-22","325-948-362" );
		map.put("2017-06-23","340-926-362" );
		map.put("2017-06-24","315-926-352" );
		map.put("2017-06-25","365-1078-340" );
		map.put("2017-06-26","325-1185-350" );
		map.put("2017-06-27","340-1200-395" );
		map.put("2017-06-28","395-1285-362" );
		map.put("2017-06-29","325-1362-355" );
		map.put("2017-06-30","346-1170-385" );
		map.put("2017-07-01","354-1262-352" );
		map.put("2017-07-02","385-1185-396" );
		map.put("2017-07-03","365-1196-400" );
		map.put("2017-07-04","385-1185-385" );
		map.put("2017-07-05","400-1451-352" );
		map.put("2017-07-06","468-1210-330" );
		map.put("2017-07-07","418-1220-350" );
		map.put("2017-07-08","421-1210-450" );
		map.put("2017-07-09","421-1195-500" );
		return map;
	}
	
/*	public static void main(S[] args) {  
		  
	    Calendar start = Calendar.getInstance();  
	    start.set(2014, 6, 11); 
	    Long startTIme = start.getTimeInMillis();  
	  
	    Calendar end = Calendar.getInstance();  
	    end.set(2014, 7, 11);  
	    Long endTime = end.getTimeInMillis();  
	  
	    Long oneDay = 1000 * 60 * 60 * 24l;  
	  
	    Long time = startTIme;  
	    while (time <= endTime) {  
	        Date d = new Date(time);  
	        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
	        System.out.println(df.format(d));  
	        time += oneDay;  
	    }  
	}  */
	


	public static List<String> findDates(Date dBegin, Date dEnd) {

		List<String> lDate = new ArrayList<String>();

		Calendar start = Calendar.getInstance();
		start.setTime(dBegin);
		Long startTIme = start.getTimeInMillis();

		Calendar end = Calendar.getInstance();
		end.setTime(dEnd);
		Long endTime = end.getTimeInMillis();

		Long oneDay = 1000 * 60 * 60 * 24l;

		Long time = startTIme;
		while (time <= endTime) {
			Date d = new Date(time);
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			lDate.add(df.format(d));
			time += oneDay;
		}
		return lDate;
	}
	
    public String queryUserReport()
    {
    	Date now = DateUtils.getDatetime();
    	
    	search.setOrderCreateTimeStart("2017-06-01");
    	search.setOrderCreateTimeEnd(DateUtils.formatDate(now));
        return SUCCESS;
    }
}
